package com.nbcio.pm.controller;

import com.nbcio.common.core.domain.R;
import com.nbcio.common.core.utils.StringUtils;
import com.nbcio.common.satoken.utils.LoginHelper;
import com.nbcio.pm.domain.vo.*;
import com.nbcio.pm.service.IPmReportStatsService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;

/**
 * 项目管理报表统计Controller
 *
 * @author nbacheng
 */
@Tag(name = "项目管理报表统计", description = "项目管理报表统计相关接口")
@RequiredArgsConstructor
@RestController
@RequestMapping("/pm/report")
public class PmReportStatsController {

    private final IPmReportStatsService reportStatsService;

    @Operation(summary = "获取项目统计", description = "获取项目总体统计信息")
    @GetMapping("/project-stats")
    public R<PmProjectStatsVo> getProjectStats() {
        String tenantId = LoginHelper.getTenantId();
        PmProjectStatsVo stats = reportStatsService.getProjectStats(tenantId);
        return R.ok(stats);
    }

    @Operation(summary = "获取任务统计", description = "获取任务统计信息")
    @GetMapping("/task-stats")
    public R<PmTaskStatsVo> getTaskStats(
            @Parameter(description = "项目ID") @RequestParam(required = false) Long projectId) {
        String tenantId = LoginHelper.getTenantId();
        PmTaskStatsVo stats = reportStatsService.getTaskStats(tenantId, projectId);
        return R.ok(stats);
    }

    @Operation(summary = "获取时间统计", description = "获取时间跟踪统计信息")
    @GetMapping("/time-stats")
    public R<PmTimeStatsVo> getTimeStats(
            @Parameter(description = "项目ID") @RequestParam(required = false) Long projectId,
            @Parameter(description = "开始日期") @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate startDate,
            @Parameter(description = "结束日期") @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endDate) {
        String tenantId = LoginHelper.getTenantId();
        PmTimeStatsVo stats = reportStatsService.getTimeStats(tenantId, projectId, startDate, endDate);
        return R.ok(stats);
    }

    @Operation(summary = "获取成员统计", description = "获取成员统计信息")
    @GetMapping("/member-stats")
    public R<PmMemberStatsVo> getMemberStats(
            @Parameter(description = "项目ID") @RequestParam(required = false) Long projectId) {
        String tenantId = LoginHelper.getTenantId();
        PmMemberStatsVo stats = reportStatsService.getMemberStats(tenantId, projectId);
        return R.ok(stats);
    }

    @Operation(summary = "获取项目进度统计", description = "获取项目进度统计列表")
    @GetMapping("/project-progress")
    public R<java.util.List<PmProjectProgressStatsVo>> getProjectProgressStats() {
        String tenantId = LoginHelper.getTenantId();
        java.util.List<PmProjectProgressStatsVo> stats = reportStatsService.getProjectProgressStats(tenantId);
        return R.ok(stats);
    }

    @Operation(summary = "获取任务状态分布统计", description = "获取任务状态分布统计列表")
    @GetMapping("/task-status")
    public R<java.util.List<PmTaskStatusStatsVo>> getTaskStatusStats(
            @Parameter(description = "项目ID") @RequestParam(required = false) Long projectId) {
        String tenantId = LoginHelper.getTenantId();
        java.util.List<PmTaskStatusStatsVo> stats = reportStatsService.getTaskStatusStats(tenantId, projectId);
        return R.ok(stats);
    }

    @Operation(summary = "获取时间跟踪统计", description = "获取时间跟踪统计列表")
    @GetMapping("/time-tracking")
    public R<java.util.List<PmTimeTrackingStatsVo>> getTimeTrackingStats(
            @Parameter(description = "项目ID") @RequestParam(required = false) Long projectId,
            @Parameter(description = "开始日期") @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate startDate,
            @Parameter(description = "结束日期") @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endDate) {
        String tenantId = LoginHelper.getTenantId();
        java.util.List<PmTimeTrackingStatsVo> stats = reportStatsService.getTimeTrackingStats(tenantId, projectId, startDate, endDate);
        return R.ok(stats);
    }

    @Operation(summary = "获取综合报表统计", description = "获取综合报表统计信息")
    @GetMapping("/comprehensive")
    public R<PmReportStatsVo> getComprehensiveStats(
            @Parameter(description = "项目ID") @RequestParam(required = false) Long projectId,
            @Parameter(description = "开始日期") @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate startDate,
            @Parameter(description = "结束日期") @RequestParam(required = false) @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate endDate) {
        String tenantId = LoginHelper.getTenantId();
        PmReportStatsVo stats = reportStatsService.getComprehensiveStats(tenantId, projectId, startDate, endDate);
        return R.ok(stats);
    }
}
